package com._51cto.healthy.content;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import java.io.Serializable;

@ApiModel("返回类型")
@Data
@NoArgsConstructor
@AllArgsConstructor
//开启链式调用
@Accessors(chain = true)
public class R<T> implements Serializable {

    private static final long serialVersionUID = 1L;
    //成功
    public static final int SUCCESS = HttpStatus.SUCCESS;
    //失败
    public static final int FAIL = HttpStatus.FAIL;

    @ApiModelProperty("响应编码")
    private Integer code;

    @ApiModelProperty("响应信息")
    private String msg;

    @ApiModelProperty("数据对象")
    private T data;

    //快速构造结果
    private static <T> R<T> result(int code, String msg, T data) {
        R<T> result = new R<>();
        result.setCode(code);
        result.setData(data);
        result.setMsg(msg);
        return result;
    }
    // 访问成功
    public static <T> R<T> success() {
        return result(SUCCESS, null, null);
    }

    public static <T> R<T> success(String msg) {
        return result(SUCCESS, msg, null);
    }

    public static <T> R<T> success(T data) {
        return result(SUCCESS, null, data);
    }

    public static <T> R<T> success(T data, String msg) {
        return result(SUCCESS, msg, data);
    }
    // 访问失败
    public static <T> R<T> fail() {
        return result(FAIL, null, null);
    }

    public static <T> R<T> fail(String msg) {
        return result(FAIL, msg, null);
    }

    public static <T> R<T> fail(T data) {
        return result(FAIL, null, data);
    }

    public static <T> R<T> fail(T data, String msg) {
        return result(FAIL, msg, data);
    }

}
